package org.jeecg.common.modules.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.jeecg.common.modules.order.entity.OrderInfo;
import org.jeecg.common.modules.order.entity.PayOrder;
import org.jeecg.common.modules.order.mapper.OrderInfoMapper;
import org.jeecg.common.modules.order.service.IOrderInfoService;
import org.jeecg.common.modules.order.vo.OrderInfoReqVo;
import org.jeecg.common.util.AmountUtils;
import org.jeecg.common.util.enums.OrderStatusEnum;
import org.jeecg.common.util.enums.ShopTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Description: order
 * @Author: jeecg-boot
 * @Date:   2020-08-26
 * @Version: V1.0
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {
    @Resource
    private  OrderInfoMapper orderInfoMapper;

    @Override
    public int countByDoctorIdAndType(String doctorId, ShopTypeEnum shopType) {
        LambdaQueryWrapper<OrderInfo> query = Wrappers.lambdaQuery();
        query.eq(OrderInfo::getDoctorId,doctorId);
        query.eq(OrderInfo::getShopType,shopType.getCode());
        query.in(OrderInfo::getStatus, Lists.newArrayList(OrderStatusEnum.DOING.getCode(),OrderStatusEnum.WAIT.getCode()));
        return count(query);
    }

    @Override
    /**
     * 查询订单信息
     */
    public List<OrderInfo> getOrderInfo(String wxUserinfoId) {
        LambdaQueryWrapper<OrderInfo> query = Wrappers.lambdaQuery();
        query.eq(OrderInfo::getWxUserinfoId,wxUserinfoId);
        return list(query);
    }
    /**
     * 查询订单信息
     */
    public List<OrderInfo> getOrderInfo(String wxUserinfoId,Integer status,Integer type) {
        LambdaQueryWrapper<OrderInfo> query = Wrappers.lambdaQuery();
        query.eq(OrderInfo::getWxUserinfoId,wxUserinfoId);
        if(Objects.nonNull(status))
        query.eq(OrderInfo::getStatus,status);
        if(Objects.nonNull(type))
        query.eq(OrderInfo::getShopType,type);
        return list(query);
    }


    @Override
    public OrderInfo initOrder(PayOrder payOrder){
        OrderInfo order = new OrderInfo();
        order.setPayOrderId(payOrder.getId());
        order.setDoctorId(payOrder.getDoctorId());
        order.setWxUserinfoId(payOrder.getWxUserinfoId());
        order.setShopType(payOrder.getShopType());
        order.setAmount(AmountUtils.fenToYuan(payOrder.getAmount()));
        order.setStatus(OrderStatusEnum.WAIT.getCode());
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setActive(false);
        save(order);
        return order;
    }

    @Override
    public List<OrderInfoReqVo> getOrderPageInfos(String id,Integer status,String userName, String doctorName, Integer dataStart, Integer pageSize){
        return orderInfoMapper.selectOrderInfo(id,status,userName,doctorName,dataStart,pageSize);
    }

    @Override
    public int getCount(String id,Integer status,String userName,String doctorName){
        return orderInfoMapper.getCount(id,status,userName,doctorName);
    }

    @Override
    public boolean isDoing(String doctorId, String wxUserinfoId,Integer shopType) {
        LambdaQueryWrapper<OrderInfo> query = Wrappers.lambdaQuery();
        query.eq(OrderInfo::getDoctorId,doctorId);
        query.eq(OrderInfo::getWxUserinfoId,wxUserinfoId);
        query.eq(OrderInfo::getShopType,shopType);
        query.in(OrderInfo::getStatus,OrderStatusEnum.getDoing());
        return count(query) > 0;
    }

    @Override
    public List<OrderInfoReqVo> excelOrderInfo(List<String> ids){
        return orderInfoMapper.excelOrderInfo(ids);
    }
}
